const BaseService = require("./base");

class Service extends BaseService {
	constructor(...args) {
		super(...args);
		this.table = "user";
	}

	async getRole(id) {
		const res = await this.app.mysql.query(
			`SELECT id, name FROM role INNER JOIN role_user ON role.id = role_user.role_id WHERE user_id = ?`,
			[id]
		);

		return res;
	}

	async setRole({ user_id, roles }) {
		await this.app.mysql.delete("role_user", {
			user_id
		});
		roles.forEach(async (role_id) => {
			await this.app.mysql.insert("role_user", {
				role_id,
				user_id
			});
		});
	}
}

module.exports = Service;
